{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d96a5c13",
   "metadata": {},
   "source": [
    "# 元组\n",
    "\n",
    "`t = (1,2,3,4)`\n",
    "\n",
    "```python\n",
    "str = \"hello\"\n",
    "t = tuple(str)\n",
    "```\n",
    "\n",
    "元组与字符串非常类似，创建后不可单独修改其中的元素，只能全部重新赋值。\n",
    "\n",
    "# 列表\n",
    "\n",
    "快速创建列表，例如`L = [x for x in range(10,101,10)]`，则`L = [10,20,30,...,100]`\n",
    "\n",
    "```python\n",
    "from random import randint\n",
    "L = [randint(0,100) for x in range(1,100)]\n",
    "```\n",
    "\n",
    "## 增删改\n",
    "\n",
    "```python\n",
    "# 增加\n",
    "L = [1,2,3]\n",
    "L.append(4) # L = [1,2,3,4]\n",
    "L.append([4,5]) # L = [1,2,3,[4,5]]\n",
    "# extend()函数，里面的参数必须是iterable，即列表、元组、字符串。追加里面的每一个元素。\n",
    "L.extend(4) # 报错\n",
    "L.extend([4,5]) # L = [1,2,3,4,5]\n",
    "```\n",
    "\n",
    "```python\n",
    "# 删除\n",
    "L[0] = [] # L = [[],2,3]，无法删除\n",
    "L[0:1] = [] # L = [2,3]，采用切片可以删除\n",
    "del L[0] # L = [2,3]，可以删除\n",
    "```\n",
    "## 遍历\n",
    "\n",
    "```python\n",
    "# 元素遍历\n",
    "L = [[1,2],[2,3],[3,4]]\n",
    "for x in L:\n",
    "    for y in x:\n",
    "        print(y)\n",
    "\n",
    "# 下标遍历\n",
    "for i in range(len(L)):\n",
    "    for j in range(len(L[i])):\n",
    "        print(L[i][j])\n",
    "```\n",
    "\n",
    "**注：若需要修改列表中的元素，则必须采用下标遍历**\n",
    "\n",
    "## 连接\n",
    "\n",
    "`join()`函数可以很方便地连接列表中的元素，但要求列表中元素必须全部为字符串，否则会报错\n",
    "\n",
    "```python\n",
    "L = [\"aaa\",\"bbb\",\"ccc\"]\n",
    "print(L.join(\",\")) # aaa,bbb,ccc\n",
    "```\n",
    "\n",
    "## 内置函数和方法\n",
    "\n",
    "| 序号 | 方法                                                         |\n",
    "| :--- | :----------------------------------------------------------- |\n",
    "| 1    | [list.append(obj)](https://www.runoob.com/python3/python3-att-list-append.html) 在列表末尾添加新的对象 |\n",
    "| 2    | [list.count(obj)](https://www.runoob.com/python3/python3-att-list-count.html) 统计某个元素在列表中出现的次数 |\n",
    "| 3    | [list.extend(seq)](https://www.runoob.com/python3/python3-att-list-extend.html) 在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表） |\n",
    "| 4    | [list.index(obj)](https://www.runoob.com/python3/python3-att-list-index.html) 从列表中找出某个值第一个匹配项的索引位置 |\n",
    "| 5    | [list.insert(index, obj)](https://www.runoob.com/python3/python3-att-list-insert.html) 将对象插入列表 |\n",
    "| 6    | [list.pop([index=-1\\])](https://www.runoob.com/python3/python3-att-list-pop.html) 移除列表中的一个元素（默认最后一个元素），并且返回该元素的值 |\n",
    "| 7    | [list.remove(obj)](https://www.runoob.com/python3/python3-att-list-remove.html) 移除列表中某个值的第一个匹配项 |\n",
    "| 8    | [list.reverse()](https://www.runoob.com/python3/python3-att-list-reverse.html) 反向列表中元素 |\n",
    "| 9    | [list.sort( key=None, reverse=False)](https://www.runoob.com/python3/python3-att-list-sort.html) 对原列表进行排序 |\n",
    "| 10   | [list.clear()](https://www.runoob.com/python3/python3-att-list-clear.html) 清空列表 |\n",
    "| 11   | [list.copy()](https://www.runoob.com/python3/python3-att-list-copy.html) 复制列表 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "12a85d0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2], [2, 3], [3, 4]]\n"
     ]
    }
   ],
   "source": [
    "L = [[1,2],[2,3],[3,4]]\n",
    "for x in L:\n",
    "    for y in x:\n",
    "        if y % 2 == 0:\n",
    "            y *= 10\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6eb7cd3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 20], [20, 3], [3, 40]]\n"
     ]
    }
   ],
   "source": [
    "# 下标遍历\n",
    "for i in range(len(L)):\n",
    "    for j in range(len(L[i])):\n",
    "        if L[i][j] % 2 == 0:\n",
    "            L[i][j] *= 10\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2f05d795",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2], [2, 3], 10, 20, [4, 5]]\n"
     ]
    }
   ],
   "source": [
    "a = [[1,2],[2,3],[3,4],[4,5]]\n",
    "a[2:3] = [10,20]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7550a86a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 0, 3]\n"
     ]
    }
   ],
   "source": [
    "x = [1,2,3]\n",
    "y = x # 注意，这样赋值是对应的同一块内存\n",
    "x[1] = 0\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "46744e6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 3, 4, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 1. 列表计算\n",
    "s1 = [0,1,2,3,4,5,6]\n",
    "s2 = s1[3:5]*3\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3030c39e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 'SUN'], [1, 'MON'], [2, 'TUE'], [3, 'WED'], [4, 'THU'], [5, 'FRI'], [6, 'SAT']]\n"
     ]
    }
   ],
   "source": [
    "# 2. 多维列表生成\n",
    "s1 = [0,1,2,3,4,5,6]\n",
    "s2 = ['SUN','MON','TUE','WED','THU','FRI','SAT']\n",
    "s5 = []\n",
    "for i in range(len(s1)):\n",
    "    s5.append([s1[i],s2[i]])\n",
    "print(s5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad5766b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 求序列的最小值（改错）\n",
    "# 输入一组逗号分隔的浮点数，输出最小值\n",
    "x = input().split(\",\")\n",
    "x = [float(i) for i in x ]\n",
    "minf = x[0]\n",
    "i  = 0\n",
    "while i < len(x):\n",
    "    if minf > x[i]:\n",
    "        minf = x[i]\n",
    "    i = i + 1\n",
    "print(\"最小元素=\", minf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ed6fe9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每位组员总分： [164, 119, 157, 174, 122, 153, 149]\n",
      "最高总分：174,小组平均分148.29\n"
     ]
    }
   ],
   "source": [
    "# 4. 计算总分和平均分\n",
    "chinese = [76,63,79,82,53,78,67]\n",
    "math = [88,56,78,92,69,75,82]\n",
    "grade_total = []\n",
    "for i in range(len(chinese)):\n",
    "    grade_total.append(chinese[i] + math[i])\n",
    "grade_avg = sum(grade_total) / len(grade_total)\n",
    "\n",
    "print(\"每位组员总分：\", grade_total)\n",
    "print(\"最高总分：{},小组平均分{:.2f}\".format(max(grade_total), grade_avg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "763c1074",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 寻找字符串中的英文字符\n",
    "a = input()\n",
    "li = []\n",
    "for i in range(0, len(a)): # 遍历字符串\n",
    "    if a[i].isalpha(): # 判断是否是英语字母\n",
    "        ch=a[i].lower() # 英文字母取小写\n",
    "        if ch not in li : # 判断字母是否在列表中\n",
    "            li.append(ch) # 将符合新的英文字母追加到列表\n",
    "for ch in li:\n",
    "  print(ch, end=\"\")    #字符连续输出，不空格，不换行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d92a2e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "符合要求的数有1个\n",
      "-725 "
     ]
    }
   ],
   "source": [
    "# 6. 寻找首位等于各位数字之和的整数\n",
    "numbers = list(input().split(\",\"))\n",
    "# numbers_new = numbers # 注意！！这个赋值方式会改变原numbers的值，它们对应的是同一块id的内存\n",
    "# numbers_new = numbers[:] # 这个赋值不会改变原列表的值\n",
    "numbers_new = numbers.copy() # 用list自带的copy函数也可以实现新开辟空间复制\n",
    "count = 0\n",
    "flag = []\n",
    "\n",
    "for i in range(len(numbers)):\n",
    "    # 判断是否为符号开头，若是，则去除符号\n",
    "    if not numbers_new[i][0].isnumeric():\n",
    "        numbers_new[i] = numbers_new[i][1:]\n",
    "    # 将去除符号后的数字转化为int类型\n",
    "    numbers_new[i] = [int(num) for num in list(numbers_new[i])]\n",
    "    # 判断\n",
    "    if sum(numbers_new[i]) == 2 * numbers_new[i][0] and len(numbers_new[i]) > 1:\n",
    "        count += 1\n",
    "        flag.append(i)\n",
    "\n",
    "print(f\"符合要求的数有{count}个\")\n",
    "for i in flag:\n",
    "    print(numbers[i], end=\" \")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
